<?php

namespace app\common\controller;

use think\facade\Db;

class Image
{

    /**
     * 商品分享海报合成
     */
    public static function createShopSharePic($qr_code,$avatar,$nickname,$shop)
    {
        //背景图片
        $path_1 = './uploads/share/bg.png';

        //照片
        $path_2= $qr_code;
        $shop_image = './uploads'.$shop['cover'];
        $avatar = './uploads'.$avatar;

        //创建图片对象
        $image_1 = imagecreatefromstring(file_get_contents($path_1));
        $image_2 = imagecreatefromstring(file_get_contents($path_2));
        $image_3 = imagecreatefromstring(file_get_contents($avatar));
        $image_4 = imagecreatefromstring(file_get_contents($shop_image));

        list($width,$height) = getimagesize($path_2);
        list($width2,$height2) = getimagesize($avatar);
        list($width3,$height3) = getimagesize($shop_image);
        //二维码
        imagecopyresampled($image_1,$image_2,
            370, //二维码向右移动
            760, //二维码向下移动
            0,
            0,
            $width/2.3, //二维码图片的宽度(按比例缩放)
            $height/2.3, //二维码图片的高度(按比例缩放)
            $width, //二维码图片的宽度
            $height //二维码图片的高度
        );
        //头像
        imagecopyresampled($image_1,$image_3,
            40,
            20,
            0,
            0,
            60,
            60,
            $width2,
            $height2
        );
        //店铺图片
        imagecopyresampled($image_1,$image_4,
            40,
            100,
            0,
            0,
            558,
            560,
            $width3,
            $height3
        );
        //这是要插入到图片的文字
        $name = $shop['goods_name'];
        $name2 = $shop['y_price'];
        $name3 = '￥';
        //文件名
        $filename="mlj";
        //设置文字颜色
        $black = imagecolorallocate($image_1, 65, 65, 65);
        $black2 = imagecolorallocate($image_1, 51, 51, 51);
        $black3 = imagecolorallocate($image_1, 255, 0, 0);

        $nickname = '来自'.$nickname.'的分享';

        //写的文字用到的字体
        $font = ROOT_PATH.'/public/static/admin/assets/font/msyh.ttf';
        //在图片里插入文字$black
        imagettftext($image_1, 20, 0, 40, 700, $black,$font, $name);
        imagettftext($image_1, 38, 0, 80, 900, $black3,$font, $name2);
        imagettftext($image_1, 22, 0, 40, 900, $black3,$font, $name3);
        imagettftext($image_1, 20, 0, 130, 60, $black2,$font, $nickname);

        //这是合成后的图片保存的路径
        $upload_dir = "./uploads/share/";
        $pic=$upload_dir.time().'.png';
        imagepng($image_1,$pic);
        return $pic;
    }

    /**
     * 健康征集海报合成
     */
    public static function createNewPic($member,$path_2,$code)
    {
        //背景图片
        $config = Db::name('config')->where('id',1)->find();
        $content = unserialize($config['content']);
        $path_1 = '.'.$content['logo'];
        //二维码

        //创建图片对象
        $image_1 = imagecreatefromstring(file_get_contents($path_1));
        $image_2 = imagecreatefromstring(file_get_contents($path_2));
        $image_3 = imagecreatefromstring(file_get_contents('.'.$member['headimgurl']));

        list($width,$height) = getimagesize($path_2);
        list($width1,$height1) = getimagesize('.'.$member['headimgurl']);
        //二维码
        imagecopyresampled($image_1,$image_2,
            332, //二维码向右移动
            685, //二维码向下移动
            0,
            0,
            $width/2, //二维码图片的宽度(按比例缩放)
            $height/2, //二维码图片的高度(按比例缩放)
            $width, //二维码图片的宽度
            $height //二维码图片的高度
        );
        //头像
        imagecopyresampled($image_1,$image_3,
            30,
            700,
            0,
            0,
            80,
            80,
            $width1,
            $height1
        );

        $name = '健康养生';
        $name1 = '长按识别二维码>>';
        $name2 = '邀请码'.$code;

        //设置文字颜色
        $black = imagecolorallocate($image_1, 65, 65, 65);
        $black1 = imagecolorallocate($image_1, 237, 64, 64);
        //写的文字用到的字体
        $font = app()->getRootPath().'/public/static/assets/font/msyh.ttf';
        imagettftext($image_1, 18, 0, 40, 880, $black,$font, $name1);
        imagettftext($image_1, 18, 0, 40, 840, $black,$font, $name);
        imagettftext($image_1, 20, 0, 120, 750, $black,$font, $member['nickname']);
        imagettftext($image_1, 20, 0, 350, 940, $black1,$font, $name2);

        //这是合成后的图片保存的路径
        $upload_dir = "./static/api/code/";
        $pic=$upload_dir.time().'.png';
        imagepng($image_1,$pic);
        return $pic;
    }

    /**
     * 推广海报合成
     */
    public static function createUserPic($path_2)
    {
        //背景图片
        $path_1 = './static/api/code/bg.png';

        //创建图片对象
        $image_1 = imagecreatefromstring(file_get_contents($path_1));
        $image_2 = imagecreatefromstring(file_get_contents($path_2));

        list($width,$height) = getimagesize($path_2);
        //二维码
        imagecopyresampled($image_1,$image_2,
            362, //二维码向右移动
            1305, //二维码向下移动
            0,
            0,
            $width/2, //二维码图片的宽度(按比例缩放)
            $height/2, //二维码图片的高度(按比例缩放)
            $width, //二维码图片的宽度
            $height //二维码图片的高度
        );

        //这是合成后的图片保存的路径
        $upload_dir = "./static/api/code/";
        $pic=$upload_dir.time().'.png';
        imagepng($image_1,$pic);
        return $pic;
    }

    public static function yuan_img($imgpath) {
        $ext     = pathinfo($imgpath);
        if ($ext['extension'] == 'jpg' || $ext['extension'] == 'jpeg') {
            $src_img = imagecreatefromjpeg($imgpath);
        } elseif ($ext['extension'] == 'png') {
            $src_img = imagecreatefrompng($imgpath);
        } else {
            return './static/api/images/logo.png';
        }
        $wh  = getimagesize($imgpath);
        //图片大小
        $fileSizeInBytes = filesize($imgpath);
        //图片不能超过1M
        if ($fileSizeInBytes >= (1 * 1024 * 1024)) {
            return './static/api/admin/images/logo.png';
        }
// dd($fileSizeInBytes);
        $w   = $wh[0];
        $h   = $wh[1];
        $w   = min($w, $h);
        $h   = $w;
        $img = imagecreatetruecolor($w, $h);
        //这一句一定要有
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r   = $w / 2; //圆半径
        $y_x = $r; //圆心X坐标
        $y_y = $r; //圆心Y坐标
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }

        $upload_dir = "./static/api/image/";
        $pic=$upload_dir.time().'.png';
        imagepng($img,$pic);
        return $pic;
    }

    /**
     * 推广海报
     */
    public static function createNewPicCode($member,$qr_code)
    {
        $path_1 = './static/api/bill/bg.png';
        //二维码
        $path_2= $qr_code;

        //创建图片对象
        $image_1 = imagecreatefromstring(file_get_contents($path_1));
        $image_2 = imagecreatefromstring(file_get_contents($path_2));
        $image_3 = imagecreatefromstring(file_get_contents($member['avatar']));

        list($width,$height) = getimagesize($path_2);
        list($width1,$height1) = getimagesize($member['avatar']);
        //二维码
        imagecopyresampled($image_1,$image_2,
            222, //二维码向右移动
            410, //二维码向下移动
            0,
            0,
            $width/1.8, //二维码图片的宽度(按比例缩放)
            $height/1.8, //二维码图片的高度(按比例缩放)
            $width, //二维码图片的宽度
            $height //二维码图片的高度
        );
        //头像
        imagecopyresampled($image_1,$image_3,
            300,
            60,
            0,
            0,
            80,
            80,
            $width1,
            $height1
        );

        $name = '邀请你加入去哪玩,扫码一起赢好礼';

        //设置文字颜色
        $black = imagecolorallocate($image_1, 65, 65, 65);
        //写的文字用到的字体
        $font = app()->getRootPath().'/public/static/assets/font/msyh.ttf';
        imagettftext($image_1, 22, 0, 125, 270, $black,$font, $name);
        imagettftext($image_1, 22, 0, 290, 185, $black,$font, $member['nickname']);

        //这是合成后的图片保存的路径
        $upload_dir = "./static/api/bill/";
        $pic=$upload_dir.time().'.png';
        imagepng($image_1,$pic);
        return $pic;
    }
}